let userInfo;
$(function () {
    $.ajax({
        headers: {
            "token": localStorage.getItem("token")
        },
        type: 'get',
        url: serverConfig.serverUrl +'/devs/user',
        async: false,//取消异步请求
        success: function (result) {
            if (result.code != 200) {
                // layer.msg('您还未登录', {icon: 2})
                localStorage.removeItem('token')
                window.location.href = '../html/login.html';
            } else {
                userInfo = result.data;
            }
        }
    })
})

$(function () {
    // 首页正在热映查询
    let hottingData = {status: 0, pageNum: 1, pageSize: 8}
    $.ajax({
        headers: {
            "token": localStorage.getItem("token")
        },
        type: 'get',
        url: serverConfig.serverUrl +'/devs/movie',
        data: hottingData,
        success: function (result) {
            let data = result.data.list;
            let str = `<div class="layui-row" id="popular_move_first_row">`;
            let s='';
            s +=`<span id="hott">正在热映 (${result.data.total}部)</span>`;
            for (let i = 0; i < data.length; i++) {
                let score = data[i].score + "";
                let scoreBig;
                let scoreShort;
                let flag = true;
                if (score == "" || score == null || score == 0) {
                    flag = false;
                } else {
                    scoreBig = score.split(".")[0];
                    scoreShort = score.split(".")[1];
                }
                str += `<div class="layui-col-md3">
                                <div class="popular_move_list">
                                    <a href="../html/moviedatas.html?id=${data[i].id}">
                                        <div class="layui-row popular_move_list_top_row" style="background: url(${"../imgs/movie-img/" + data[i].posterImg}) no-repeat;background-size: 160px 220px;">
                                          
                                            <div class="movie-info">
                                                <div class="movie-score">`
                if (flag) {
                    str += `
                                                    <i class="integer">${scoreBig + "."}</i>
                                                    <i class="fraction">${scoreShort}</i>`
                } else {
                    str += `
                                                    <i class="fraction">暂无评分</i>`
                }
                str += `
                                                </div>
                                                <div class="movie-title">
                                                    ${data[i].title}
                                                </div>
                                            </div>
                                            
                                        </div>
                                    </a>
                                    <div class="layui-row popular_move_list_botten_row">
                                        <a href="../html/moviedatas.html?id=${data[i].id}">购&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;票</a>
                                    </div>
                                </div>
                            </div>`
                if (i == 3) {
                    str += `</div>
                        <div class="layui-row" id="popular_move_second_row">`;
                }
            }
            str += `</div>`;
            document.getElementById("popular_move_box").innerHTML = str;
            document.getElementById("popular_more_col_left").innerHTML=s;
        }
    })

    // 首页今日票房查询
    topMovie();

    // 预售
    let desireData = {status: 1, pageNum: 1, pageSize: 8}
    $.ajax({
        type: 'get',
        url: serverConfig.serverUrl + '/devs/movie',
        data: desireData,
        success: function (result) {
            console.log(result)
            let data = result.data.list;
            let str = `<div class="layui-col-md12 upcoming_box_first_row">`;
            let str1='';
            str1+=` <span>即将上映 (${result.data.total}部)</span>`
            for (let i = 0; i < data.length; i++) {
                str += `<div class="layui-col-md3">
                            <div class="popular_move_list">
                                <a href="#">
                                    <div class="layui-row upcoming_box_list" style="background: url(${"../imgs/movie-img/" + data[i].posterImg}) no-repeat;background-size: 160px 220px;">
                                        <div class="upcoming_list_font_box">
                                            <div class="upcoming_list_font">${data[i].title}</div>
                                        </div>
                                    </div>
                                </a>
                                <div class="upcoming_list_work">
                                    <span>${data[i].desire}</span>
                                    人想看
                                </div>
                                <div class="upcoming_list_work_btn">
                                    <a class="upcoming_list_work_btn_left">预告片</a>
                                    <a class="upcoming_list_work_btn_right">预售</a>
                                </div>
                                <div class="popular_move_list_data">${data[i].firstDate}上映</div>
                            </div>
                        </div>`
                if (i == 3) {
                    str += `</div>
                    <div class="layui-col-md12 upcoming_box_first_row" id="upcoming_box_first_row_orther">`
                }
            }
            str += `</dev>`;
            document.getElementById("upcoming_box").innerHTML = str;
            document.getElementById("upcoming_row_top_left").innerHTML = str1;
        }
    })

    $.ajax({
        type: 'get',
        url: serverConfig.serverUrl +'/devs/movie/desire',
        success: function (result) {
            // console.log(result)
            let data = result.data;
            // console.log(data)
            let str = `<div class="layui-row" id="upcoming_row">`
            str += `<div class="layui-col-md12 layui-col-md-offset1" id="before_rank"><p>最受期待<span>查看完整榜单<img src="../imgs/icon/jian.png"></span></p></div>
                <div class="layui-col-md12 layui-col-md-offset1" id="want_one" onclick="wantSee(${data[0].id})">
                    <div><img src="../imgs/movie-img/${data[0].posterImg}" class="img-one"><div class="div-littleOne"><img src="../imgs/icon/hot1.png"></div></div>
                    <div class="nameOne-div"><p>${data[0].title}</p></div>
                    <div class="timeOne-div"><p>上映时间: ${data[0].firstDate.split(" ")[0]}</p></div>
                    <div class="wantOne-div"><p>${data[0].desire}人想看</p></div>
                </div>
               <!--  2 -- 3 的排名的想看   -->
                <div class="layui-col-md12 layui-col-md-offset1" id="middle_rank">
                    <div class="layui-col-md5" id="want_two" onclick="wantSee(${data[1].id})">
                        <div class="layui-col-md12"><img src="../imgs/movie-img/${data[1].posterImg}" class="img-two"><div class="div-littleTwo"><img src="../imgs/icon/two.png"></div></div>
                        <div class="layui-col-md12 nameTwo-div"><p>${data[1].title}</p></div>
                        <div class="layui-col-md12 wantTwo-div"><p>${data[1].desire}人想看</p></div>
                    </div>
                    <div class="layui-col-md5" id="want_three" onclick="wantSee(${data[2].id})">
                        <div class="layui-col-md12"><img src="../imgs/movie-img/${data[2].posterImg}" class="img-three"><div class="div-littleThree"><img src="../imgs/icon/three.png"></div></div>
                        <div class="layui-col-md12 nameThree-div"><p>${data[2].title}</p></div>
                        <div class="layui-col-md12 wantThree-div"><p>${data[2].desire}人想看</p></div>
                    </div>
                </div><!-- 4 -- 10 的排名的想看 -->
                <div class="layui-col-md12 layui-col-md-offset1" id="after_rank">
                    <div class="layui-col-md12" id="want_four" onclick="wantSee(${data[3].id})">
                        <div class="layui-col-md1"><span class="rank"><p>4</p></span></div>
                        <div class="layui-col-md7"><span class="mov-name"><p>${data[3].title}</p></span></div>
                        <div class="layui-col-md3 layui-col-md-offset1"><span class="wan-name"><p>${data[3].desire}想看</p></span></div>
                    </div>
                    <div class="layui-col-md12" id="want_five" onclick="wantSee(${data[4].id})">
                        <div class="layui-col-md1"><span class="rank"><p>5</p></span></div>
                        <div class="layui-col-md7"><span class="mov-name"><p>${data[4].title}</p></span></div>
                        <div class="layui-col-md3 layui-col-md-offset1"><span class="wan-name"><p>${data[4].desire}想看</p></span></div>
                    </div>
                    <div class="layui-col-md12" id="want_six" onclick="wantSee(${data[5].id})">
                        <div class="layui-col-md1"><span class="rank"><p>6</p></span></div>
                        <div class="layui-col-md7"><span class="mov-name"><p>${data[5].title}</p></span></div>
                        <div class="layui-col-md3 layui-col-md-offset1"><span class="wan-name"><p>${data[5].desire}想看</p></span></div>
                    </div>
                    <div class="layui-col-md12" id="want_seven" onclick="wantSee(${data[6].id})">
                        <div class="layui-col-md1"><span class="rank"><p>7</p></span></div>
                        <div class="layui-col-md7"><span class="mov-name"><p>${data[6].title}</p></span></div>
                        <div class="layui-col-md3 layui-col-md-offset1"><span class="wan-name"><p>${data[6].desire}想看</p></span></div>
                    </div>
                    <div class="layui-col-md12" id="want_eight" onclick="wantSee(${data[7].id})">
                        <div class="layui-col-md1"><span class="rank"><p>8</p></span></div>
                        <div class="layui-col-md7"><span class="mov-name"><p>${data[7].title}</p></span></div>
                        <div class="layui-col-md3 layui-col-md-offset1"><span class="wan-name"><p>${data[7].desire}想看</p></span></div>
                    </div>
                    <div class="layui-col-md12" id="want_nine" onclick="wantSee(${data[8].id})">
                        <div class="layui-col-md1"><span class="rank"><p>9</p></span></div>
                        <div class="layui-col-md7"><span class="mov-name"><p>${data[8].title}</p></span></div>
                        <div class="layui-col-md3 layui-col-md-offset1"><span class="wan-name"><p>${data[8].desire}想看</p></span></div>
                    </div>
                    <div class="layui-col-md12" id="want_ten" onclick="wantSee(${data[9].id})">
                        <div class="layui-col-md1"><span class="rank"><p>10</p></span></div>
                        <div class="layui-col-md7"><span class="mov-name"><p>${data[9].title}</p></span></div>
                        <div class="layui-col-md3 layui-col-md-offset1"><span class="wan-name"><p>${data[9].desire}想看</p></span></div>
                    </div>
                </div>`
            str += `</dev>`;
            document.getElementById("most_anticipated").innerHTML = str;
        }
    })
});

function wantSee(movieId) {
    window.location.href = "../html/moviedatas.html?id=" + movieId;
}

//
function topMovie() {
    $.ajax({
        type: 'get',
        url: serverConfig.serverUrl + `/devs/movie/sorce`,
        success: function (result) {
            let data = result.data;
            let str = '';
            for (let i = 0; i < data.length; i++) {
                if (i == 0) {
                    str += `<a href="../html/moviedatas.html?id=${data[i].id}">
                            <div class="layui-row today_vote_list_big today_vote_list_color">
                                <div class="layui-col-md4" id="today_vote_img_box">
                                    <img src=${"../imgs/movie-img/" + data[i].posterImg} alt="">
                                    <i class="layui-icon layui-icon-fire"></i>
                                </div>
                                <div class="layui-col-md7">
                                    <div class="layui-row" id="today_vote_list_big_name">
                                        <span>${data[i].title}</span>
                                    </div>
                                    <div class="layui-row" id="today_vote_list_big_value">
                                        <span>${data[i].grossed}</span>
                                        <span class="cny">万</span>
                                    </div>
                                </div>
                            </div>
                        </a>`
                } else {
                    str += `<a href="../html/moviedatas.html?id=${data[i].id}">
                            <div class="layui-row today_vote_list today_vote_list_color">
                                <div class="layui-col-md8 today_vote_list_name">
                                    <span class="today_vote_list_name_sort">${i + 1}</span>
                                    <span class="today_vote_list_name_movie">${data[i].title}</span>
                                </div>
                                <div class="layui-col-md3 today_vote_list_value">
                                    <span>${data[i].grossed}</span>
                                    <span>万</span>
                                </div>
                            </div>
                        </a>`
                }
            }
            document.getElementById("today_list").innerHTML = str;
        }
    })
}

// 每10秒刷醒一次
let ms = setInterval(topMovie, 1000);


